package com.jingdianjichi.practice.server.serverce.impl;

import com.jingdianjichi.api.subject.SubjectClient;
import com.jingdianjichi.api.subject.entity.SpecialPracticeVO;
import com.jingdianjichi.api.subject.entity.dto.PracticeSubjectDTO;
import com.jingdianjichi.practice.server.entity.po.PracticeDetailPO;
import com.jingdianjichi.practice.server.entity.po.PracticePO;
import com.jingdianjichi.practice.server.entity.po.PracticeSetDetailPO;
import com.jingdianjichi.practice.server.entity.po.PracticeSetPO;
import com.jingdianjichi.practice.server.entity.vo.PracticeSubjectListVO;
import com.jingdianjichi.api.subject.entity.vo.PracticeSubjectVO;
import com.jingdianjichi.practice.server.enums.CompleteStatusEnum;
import com.jingdianjichi.practice.server.enums.IsDeletedFlagEnum;
import com.jingdianjichi.practice.server.mapper.PracticeDao;
import com.jingdianjichi.practice.server.mapper.PracticeDetailDao;
import com.jingdianjichi.practice.server.mapper.PracticeSetDao;
import com.jingdianjichi.practice.server.mapper.PracticeSetDetailDao;
import com.jingdianjichi.practice.server.serverce.PracticeSetService;
import com.jingdianjichi.practice.server.entity.vo.PracticeSetVO;
import com.jingdianjichi.api.subject.entity.vo.PracticeSubjectDetailVO;
import com.jingdianjichi.practice.server.util.LoginUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
public class PracticeSetServiceImpl implements PracticeSetService {

    @Resource
    private SubjectClient subjectClient;
    @Resource
    private PracticeSetDao practiceSetDao;
    @Resource
    private PracticeSetDetailDao practiceSetDetailDao;
    @Resource
    private PracticeDetailDao practiceDetailDao;
    @Resource
    private PracticeDao practiceDao;





    @Override
    public List<SpecialPracticeVO> getSpecialPracticeContent() {
        List<SpecialPracticeVO> specialPracticeVOList = subjectClient.getSpecialPracticeContent();
        return specialPracticeVOList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PracticeSetVO addPractice(PracticeSubjectDTO practiceSubjectDTO) {
        PracticeSetVO practiceSetVO = new PracticeSetVO();
        //获取套卷题目信息
        List<PracticeSubjectDetailVO> practiceList = subjectClient.getPracticeList(practiceSubjectDTO);
        if (CollectionUtils.isEmpty(practiceList)) {
            return practiceSetVO;
        }

        //构建套卷名称,大类id
        Map<String, Object> info = subjectClient.getPracticeName(practiceSubjectDTO);
        String practiceName = String.valueOf(info.get("practiceName"));
        Long categoryId = Long.valueOf(info.get("categoryId").toString());

        PracticeSetPO practiceSetPO = new PracticeSetPO();
        practiceSetPO.setSetType(1);
        practiceSetPO.setSetName(practiceName);
        practiceSetPO.setPrimaryCategoryId(categoryId);
        practiceSetPO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        practiceSetPO.setCreatedBy(LoginUtil.getLoginId());
        practiceSetPO.setCreatedTime(new Date());
        practiceSetDao.add(practiceSetPO);

        Long practiceSetId = practiceSetPO.getId();

        //TODO 后期优化，一次装入数据库
        practiceList.forEach(e -> {
            PracticeSetDetailPO detailPO = new PracticeSetDetailPO();
            detailPO.setSetId(practiceSetId);
            detailPO.setSubjectId(e.getSubjectId());
            detailPO.setSubjectType(e.getSubjectType());
            detailPO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            detailPO.setCreatedBy(LoginUtil.getLoginId());
            detailPO.setCreatedTime(new Date());
            practiceSetDetailDao.add(detailPO);
        });
        practiceSetVO.setSetId(practiceSetId);
        return practiceSetVO;
    }

    /**
     * 获取练习题
     */
    @Override
    public PracticeSubjectListVO getSubjects(PracticeSetVO practiceSetVO) {
        Long setId = practiceSetVO.getSetId();
        PracticeSubjectListVO vo = new PracticeSubjectListVO();
        List<PracticeSubjectDetailVO> practiceSubjectListVOS = new LinkedList<>();
        //查询题目类型，题目id
        List<PracticeSetDetailPO> practiceSetDetailPOS = practiceSetDetailDao.selectBySetId(setId);
        if (CollectionUtils.isEmpty(practiceSetDetailPOS)) {
            return vo;
        }
        String loginId = LoginUtil.getLoginId();
        Long practiceId = practiceSetVO.getPracticeId();
        practiceSetDetailPOS.forEach(PracticeSetDetailPO -> {
            PracticeSubjectDetailVO practiceSubjectListVO = new PracticeSubjectDetailVO();
            practiceSubjectListVO.setSubjectId(PracticeSetDetailPO.getSubjectId());
            practiceSubjectListVO.setSubjectType(PracticeSetDetailPO.getSubjectType());
            if (Objects.nonNull(practiceId)) {
                PracticeDetailPO practiceDetailPO = practiceDetailDao.selectDetail(practiceId, PracticeSetDetailPO.getSubjectId(), loginId);
                if (Objects.nonNull(practiceDetailPO) && StringUtils.isNotBlank(practiceDetailPO.getAnswerContent())) {
                    practiceSubjectListVO.setIsAnswer(1);
                } else {
                    practiceSubjectListVO.setIsAnswer(0);
                }
            }
            practiceSubjectListVOS.add(practiceSubjectListVO);
        });
        vo.setSubjectList(practiceSubjectListVOS);
        PracticeSetPO practiceSetPO = practiceSetDao.selectById(setId);
        vo.setTitle(practiceSetPO.getSetName());


        if (Objects.isNull(practiceId)) {
            Long newPracticeId = insertUnCompletePractice(setId);
            vo.setPracticeId(newPracticeId);
        } else {
            updateUnCompletePractice(practiceId);
            PracticePO practicePO = practiceDao.selectById(practiceId);
            vo.setTimeUse(practicePO.getTimeUse());
            vo.setPracticeId(practiceId);
        }
        return vo;
    }

    private Long insertUnCompletePractice(Long practiceSetId) {
        PracticePO practicePO = new PracticePO();
        practicePO.setSetId(practiceSetId);
        practicePO.setCompleteStatus(CompleteStatusEnum.NO_COMPLETE.getCode());
        practicePO.setTimeUse("00:00:00");
        practicePO.setSubmitTime(new Date());
        practicePO.setCorrectRate(new BigDecimal("0.00"));
        practicePO.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        practicePO.setCreatedBy(LoginUtil.getLoginId());
        practicePO.setCreatedTime(new Date());
        practiceDao.insert(practicePO);
        return practicePO.getId();
    }

    private void updateUnCompletePractice(Long practiceId) {
        PracticePO practicePO = new PracticePO();
        practicePO.setId(practiceId);
        practicePO.setSubmitTime(new Date());
        practiceDao.update(practicePO);
    }
    /**
     * 获取题目详情
     * @param practiceSubjectDTO
     * @return
     */
    @Override
    public PracticeSubjectVO getPracticeSubject(PracticeSubjectDTO practiceSubjectDTO) {
        PracticeSubjectVO practiceSubjectVO=subjectClient.getPracticeSubject(practiceSubjectDTO);
        return practiceSubjectVO;

    }
}